package com.wanxi.service.impl;

import com.wanxi.dao.UserDao;
import com.wanxi.model.Permission;
import com.wanxi.model.ResultModel;
import com.wanxi.model.UserModel;
import com.wanxi.service.UserService;
import com.wanxi.util.Md5;
import com.wanxi.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;

/**
 * @author 杨金乾
 */
@Service
public class UserServiceImpl implements UserService, UserDetailsService {
    @Autowired
    UserDao userDao;
    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public ResultModel enable(UserModel model) {
        return ResultModel.getResult(userDao.enable(model));
    }

    @Override
    public ResultModel add(UserModel model) {
        String yuanmima = model.getPassword();
        String jiamimima = Md5.encoderByMd5(yuanmima);
//        调用dao层之前，需要对密码进行加密
        model.setPassword(jiamimima);

        SnowflakeIdWorker snowflakeIdWorker=new SnowflakeIdWorker(WorkId.WORK_ID);
        model.setId(snowflakeIdWorker.nextId());
        return ResultModel.getResult(userDao.add(model));
    }

    @Override
    public ResultModel update(UserModel model) {

        return ResultModel.getResult(userDao.update(model));
    }

    @Override
    public ResultModel delete(UserModel model) {

        return ResultModel.getResult(userDao.delete(model));
    }

    @Override
    public ResultModel findById(UserModel model) {

        return ResultModel.getResult(userDao.findById(model));
    }

    @Override
    public ResultModel findAll(UserModel model) {

        model.setLimit(10);
        model.setPage((model.getPage()-1)*10);
        return ResultModel.getResult(
                userDao.findAll(model),
                userDao.count(model));
    }

    @Override
    public ResultModel count(UserModel model) {
        return ResultModel.getResult(userDao.count(model));
    }

    @Override
    public ResultModel isShow(UserModel userModel) {
        return null;
    }

    @Override
    public ResultModel isRecommend(UserModel userModel) {
        return null;
    }

    @Override
    public ResultModel whereShow(UserModel userModel) {
        return null;
    }

    @Override
    public ResultModel login(UserModel userModel) {
        String yuanmima = userModel.getPassword();
        String jiamimima = Md5.encoderByMd5(yuanmima);
//        调用dao层之前，需要对密码进行加密
        userModel.setPassword(jiamimima);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Object value=  valueOperations.get(userModel.getUsername());
        if (value == null) {
            UserModel model = userDao.findByUsernameAndPassword(userModel);
            if (model != null) {
                valueOperations.set(userModel.getUsername(), userModel.getPassword());
                return ResultModel.success();
            } else {
                return ResultModel.error();
            }
        }else {
            if (jiamimima.equals(value)){
                return ResultModel.success();
            }
        }
        return ResultModel.error();
    }



    @Override
    public ResultModel password(UserModel model) {
        /**
         * 1.首先判断旧密码是否一致
         * 2.如果一致则修改新密码
         * 3.如果不一致，则提示返回影响的条数为-1
         */
        model.setNewPassword(Md5.encoderByMd5(model.getNewPassword()));
        model.setPassword(Md5.encoderByMd5(model.getPassword()));

        // 这里是用的以前登录的代码
        UserModel userModel = userDao.findByUsernameAndPassword(model);
        if (userModel == null) {
            return ResultModel.getResult(-1);
        } else {
            return ResultModel.getResult(userDao.updatePassword(model));
        }

    }

    @Override
    public UserModel getUserByName(String username) {
        // 获取账户名对应的用户对象
        List<UserModel> users= userDao.getUserByName(username);
        Assert.isTrue(users.size()==1,"您输入的账户不存在，或者有多个相同的账户");
        return users.get(0);
    }

    @Override
    public List<Permission> getPermissionsByUserId(Long id) {
        return userDao.getPermissionByUserId(id);
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserModel userModel=getUserByName(username);
        List<Permission> permissionList=getPermissionsByUserId(userModel.getId());
        HashSet<Permission> permissions=new HashSet<>(permissionList);
        userModel.setAuthorities(permissions);

        return userModel;
    }
}
